package com.laiketui.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laiketui.order.api.enums.OrderDetailEnum;
import com.laiketui.order.api.enums.OrderStatusEnum;
import com.laiketui.order.domain.Order;
import com.laiketui.order.domain.OrderDetail;
import com.laiketui.order.service.OrderDetailService;
import com.laiketui.order.mapper.OrderDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @author jiangxiang
* @description 针对表【lk_order_detail(订单子项表)】的数据库操作Service实现
* @createDate 2024-08-16 22:21:45
*/
@Service
@Slf4j
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail>
    implements OrderDetailService{



    @Override
    public boolean updateStatusCancel(String orderParentNo) {
        log.info("开始取消订单子项:{}",orderParentNo);
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getOrderParentNo, orderParentNo);
        wrapper.eq(OrderDetail::getStatus, OrderDetailEnum.WAIT_PAY.getCode());
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setUpdateBy("系统取消订单");
        orderDetail.setUpdateTime(new Date());
        orderDetail.setStatus(OrderDetailEnum.CANCEL.getCode());
        boolean update = update(orderDetail, wrapper);
        log.info("取消订单子项:{}",update);
        return update;
    }

    @Override
    public List<OrderDetail> findByOrderParentNo(String orderParentNo) {
        LambdaQueryWrapper<OrderDetail> wrapper=new LambdaQueryWrapper<OrderDetail>();
        wrapper.eq(OrderDetail::getOrderParentNo,orderParentNo);
        return list(wrapper);
    }

    @Override
    public List<OrderDetail> findByIds(List<String> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<OrderDetail> wrapper=new LambdaQueryWrapper<OrderDetail>();
        wrapper.in(OrderDetail::getId,ids);
        return list(wrapper);
    }

    @Override
    public List<OrderDetail> findByOrderNo(String orderNo) {
        LambdaQueryWrapper<OrderDetail> wrapper=new LambdaQueryWrapper<OrderDetail>();
        wrapper.eq(OrderDetail::getOrderNo,orderNo);
        return list(wrapper);
    }

    @Override
    public void confirmByOrderNo(String orderNo,String userId) {
        LambdaQueryWrapper<OrderDetail> wrapper=new LambdaQueryWrapper<OrderDetail>();
        wrapper.eq(OrderDetail::getOrderNo,orderNo);
        OrderDetail detail = new OrderDetail();
        detail.setStatus(OrderDetailEnum.WAIT_CHECK.getCode());
        detail.setUpdateBy(userId);
        detail.setUpdateTime(new Date());
        update(detail,wrapper);
    }

    @Override
    public boolean updateStatusFinish(String orderNo, String userId) {
        log.info("开始完成订单子项:{}",orderNo);
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getOrderNo, orderNo);
        wrapper.eq(OrderDetail::getStatus, OrderDetailEnum.WAIT_SEND.getCode());
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setUpdateBy(userId);
        orderDetail.setUpdateTime(new Date());
        orderDetail.setStatus(OrderDetailEnum.WAIT_RECEIVE.getCode());
        boolean update = update(orderDetail, wrapper);
        log.info("完成订单子项:{}",update);
        return update;
    }

    @Override
    public OrderDetail findByOrderDetailNo(String detailNo) {
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getDetailNo, detailNo);
        return getOne(wrapper);
    }
}




